package com.tsingteng.cosfun.widget.dialog;

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BaseDialogFragment;
import com.tsingteng.cosfun.utils.AppUtils;


public class AudioMixSettingDialog extends BaseDialogFragment {

    private int mFgVolume = 100;
    private int mBgVolume = 100;

    private SeekBar mSrcVolumeSeekBar;
    private SeekBar mMixVolumeSeekBar;
    private SeekBar mMixPositionSeekBar;
    private OnAudioMixSettingDialogDissMiss onAudioMixSettingDialogDissMiss;

    private OnPositionSelectedListener mOnPositionSelectedListener;

    private OnAudioVolumeChangedListener mOnAudioVolumeChangedListener;

    public interface OnAudioVolumeChangedListener {
        void onAudioVolumeChanged(int fgVolume, int bgVolume);
    }

    public interface OnPositionSelectedListener {
        void onPositionSelected(long position);
    }

    @Override
    protected int getTopOffset() {
        return  AppUtils.getScreenWidthAndHeight(getActivity())[1] / 100 * 80;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_audio_mix,container,false);
        initView(view);
        return view;
    }

    @Override
    protected void initView() {

    }


    protected void initView(View view) {
        ImageView mClose = view.findViewById(R.id.iv_close);
        mClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onAudioMixSettingDialogDissMiss!= null)
                    onAudioMixSettingDialogDissMiss.onDismiss();
            }
        });
        mSrcVolumeSeekBar = (SeekBar)view.findViewById(R.id.fg_volume);
        mSrcVolumeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mFgVolume = progress;
                mOnAudioVolumeChangedListener.onAudioVolumeChanged(mFgVolume, mBgVolume);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });


        mSrcVolumeSeekBar.getThumb().setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_ATOP);
        mMixVolumeSeekBar = (SeekBar) view.findViewById(R.id.bg_volume);
        mMixVolumeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mBgVolume = progress;
                mOnAudioVolumeChangedListener.onAudioVolumeChanged(mFgVolume, mBgVolume);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        mMixVolumeSeekBar.getThumb().setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_ATOP);

        mMixPositionSeekBar = (SeekBar) view.findViewById(R.id.mix_position);
        mMixPositionSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                long seekTime = seekBar.getProgress();
                mOnPositionSelectedListener.onPositionSelected(seekTime);
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.dialog_audio_mix;
    }

    public void setSrcVolumeProgress(int progress) {
        mSrcVolumeSeekBar.setProgress(progress);
    }

    public int getSrcVolumeProgress() {
        return mSrcVolumeSeekBar.getProgress();
    }

    public void setMixVolumeProgress(int progress) {
        mMixVolumeSeekBar.setProgress(progress);
    }

    public void setMixAudioMaxVolume(int maxVolume) {
        mMixVolumeSeekBar.setMax(maxVolume);
    }

    public void setMixPosition(int position) {
        mMixPositionSeekBar.setProgress(position);
    }

    public void setMixMaxPosition(int maxPosition) {
        mMixPositionSeekBar.setMax(maxPosition);
    }

    public void clearMixAudio() {
        setMixPosition(0);
        setSrcVolumeProgress(100);
        setMixAudioMaxVolume(100);
    }

    public void setOnPositionSelectedListener(OnPositionSelectedListener listener) {
        mOnPositionSelectedListener = listener;
    }

    public void setOnAudioVolumeChangedListener(OnAudioVolumeChangedListener listener) {
        mOnAudioVolumeChangedListener = listener;
    }




    public void setOnAudioMixSettingDialogDissMiss(OnAudioMixSettingDialogDissMiss onAudioMixSettingDialogDissMiss) {
        this.onAudioMixSettingDialogDissMiss = onAudioMixSettingDialogDissMiss;
    }
    public interface OnAudioMixSettingDialogDissMiss {
        void onDismiss();
    }

}
